package com.itheima.service.impl;

import cn.itheima.health.service.OrderSettingService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class OrderSettingsServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    @Override
    public void importOrderSettings(List<OrderSetting> orderSettingsList) {

        //业务层中  就是将 封装好的数据循环遍历出来，一边遍历  一边放到 orderSettingsMapper中
        for (OrderSetting orderSetting : orderSettingsList) {

            //  判断 根据日期查询 表中是否存在已经上传过的记录  如果已经存在就update 如果不存在 就insert
                    // getOrderDate  :获取订单日期
              int num =  orderSettingMapper.findOrderSettingByOrderDate(orderSetting.getOrderDate());
                if (num>0){
                    //update xxx set number = number + xxx where orderdate = xxx
                    orderSettingMapper.updateOrderSettingByOrderDate(orderSetting);
                }
                else {
                    orderSettingMapper.importOrderSettings(orderSetting);
                }
        }
    }

    @Override
    public List<Map> findOrderSettingByDate(int year, int month) {
       String beginDate = year+"-"+month+"-1";
       String endDate = year+"-"+month+"-31";
       List<OrderSetting> list =orderSettingMapper.findOrderSettingByDate(beginDate,endDate);
        List ListMap = new ArrayList<>();
        //响应json 格式数据
        //设置遍历查询的日期
        for (OrderSetting orderSetting:list){
            Map map =new HashMap<>();
            Date orderDate = orderSetting.getOrderDate();
            map.put("date",orderDate.getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            ListMap.add(map);
        }
        return ListMap;
    }

    @Override
    public void updateOrderSettingData(String dateparam, int num) {
        orderSettingMapper.updateOrderSettingData(dateparam,num);
    }
}
